package com.java.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.domain.event.ActivityLawEvent;
import com.java.domain.event.ActivityReviewEvent;
import com.java.domain.event.ActivitySlotEvent;
import com.java.domain.event.ActivitySportEvent;
import com.java.entity.AboutUser;
import com.java.service.LawService;
import com.java.service.RecordService;
import com.java.service.ReviewService;
import com.java.service.SlotService;
import com.java.service.SportService;
import com.java.util.MyTools;
import com.java.util.interceptor.LoginUser;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@Controller
public class IndexController {
	@Autowired private SportService sportService;
	@Autowired private LawService lawService;
	@Autowired private SlotService slotService;
	@Autowired private RecordService recordService;
	@Autowired private ReviewService reviewService;

	@RequestMapping(value = { "", "/" }, method = RequestMethod.GET)
	public String index(Model model) {
		TableRespone table = sportService.getListByLast();
		model.addAttribute("table", table);
		return "index";
	}

	@RequestMapping(value = "/law", method = RequestMethod.GET)
	public String law(Model model) {
		ActivityLawEvent law = lawService.getOneByCode("INDEXLAW");
		model.addAttribute("law", law);
		return "law";
	}

	@RequestMapping(value = "/serviceLaw", method = RequestMethod.GET)
	public String serviceLaw(Model model) {
		ActivityLawEvent law = lawService.getOneByCode("SERVICELAW");
		model.addAttribute("law", law);
		return "law";
	}

	@RequestMapping(value = "/accountLaw", method = RequestMethod.GET)
	public String accountLaw(Model model) {
		ActivityLawEvent law = lawService.getOneByCode("ACCOUNTLAW");
		model.addAttribute("law", law);
		return "law";
	}

	@RequestMapping(value = "/about", method = RequestMethod.GET)
	public String about(Model model) {
		List<AboutUser> users = new ArrayList<AboutUser>();
		users.add(new AboutUser("zhangsan", "/assets/images/team/01.jpg"));
		users.add(new AboutUser("lisi", "/assets/images/team/02.jpg"));
		users.add(new AboutUser("wangwu", "/assets/images/team/03.jpg"));
		users.add(new AboutUser("zhaoliu", "/assets/images/team/04.jpg"));
		model.addAttribute("users", users);
		return "about";
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model,
			@RequestParam(value="type", defaultValue = "", required = false) Integer type, 
			@RequestParam(value="title", defaultValue = "", required = false) String title, 
			@RequestParam(value="page", defaultValue = "0", required = false) int page, 
			@RequestParam(value="size", defaultValue = "10", required = false) int size) {
		TableRespone table = sportService.getListByType(type, title, 1, page, size);
		model.addAttribute("table", table);
		model.addAttribute("type", MyTools.isEmpty(type)?"":type);
		model.addAttribute("title", MyTools.isEmpty(title)?"":title);
		model.addAttribute("page", page);
		model.addAttribute("size", size);
		model.addAttribute("total", table.getTotal());
		return "list";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/detail/{sportId}", method = RequestMethod.GET)
	public String detail(Model model, @PathVariable String sportId) {
		List<ActivitySlotEvent> slots = new ArrayList<ActivitySlotEvent>();
		List<ActivityReviewEvent> reviews = new ArrayList<ActivityReviewEvent>();
		ActivitySportEvent sport = sportService.getOne(sportId);
		model.addAttribute("sport", sport);
		TableRespone stable = slotService.getListBySport(sportId, 1);
		if(stable.getTotal() > 0) {
			slots = (List<ActivitySlotEvent>) stable.getRows();
		}
		model.addAttribute("slots", slots);
		TableRespone rtable =  reviewService.getReviewsBySport(sportId, 0, 0);
		if(rtable.getTotal() > 0) {
			reviews = (List<ActivityReviewEvent>) rtable.getRows();
		}
		model.addAttribute("reviews", reviews);
		model.addAttribute("nowDateStr", MyTools.getDateStr(new Date()));
		return "detail";
	}

	@RequestMapping(value = "/createOrder", method = RequestMethod.POST)
	@ResponseBody
	public BaseRespone createOrder(String sportId, String slotId, String date) {
		if(MyTools.isEmpty(ServletUtil.getUserId())) {
			return BaseRespone.error("Unauthorized");
		}
		return recordService.createOrder(sportId, slotId, ServletUtil.getUserId(), date);
	}

	@RequestMapping(value = "/saveReview", method = RequestMethod.POST)
	@ResponseBody
	public BaseRespone saveReview(String sportId, String reviewText) {
		if(MyTools.isEmpty(ServletUtil.getUserId())) {
			return BaseRespone.error("Unauthorized");
		}
		return reviewService.saveReview(ServletUtil.getUserId(), sportId, reviewText);
	}

	@RequestMapping(value = "/getSlot", method = RequestMethod.GET)
	@ResponseBody
	public ActivitySlotEvent getSlot(String slotId, String date) {
		return slotService.getByDate(slotId, date);
	}

	@RequestMapping(value = "/data", method = RequestMethod.GET)
	@ResponseBody
	public TableRespone data(int page, int size) {
		List<LoginUser> list = new ArrayList<LoginUser>();
		list.add(new LoginUser("id123", "user", "张三", null, null));
		return new TableRespone(list, 1);
	}
}